// pages/defect/adjustment/main/formula/index.js
import {
  _add,
  _list,
  _update,
  _remove
} from '../../../../../api/defect-adjustment-ingredients';
import {
  _list as ingParentList
} from '../../../../../api/ingredients-parent'
import {
  list
} from '../.../../../../../../api/ingredients'
let idex;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {},
      observer(data) {

        data?.['id'] && this.fetchList(data['id'])
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataSource: [],
    ingredientsList: [],
    ingredientsChildList: [],
    ingredientShow: false,
    modalShow: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onAdd() {

      this.onModalShow();
    },
    onInput(event) {
      const {
        index,
        key
      } = event.currentTarget.dataset,
        dataSource = this.data.dataSource,
        value = String(dataSource[index][key]);
      wx.showModal({
        title: dataSource[index]['_info']['name'],
        editable: true,
        content: value,
        placeholderText: '输入值',
        success: res => {
          if (res.confirm) {
            _update({
              defectAdjustmentId: dataSource[index]['defectAdjustmentId'],
              id: dataSource[index]['id'],
              weight: res.content,
              ingredientsId: dataSource[index]['ingredientsId']
            }, () => {
              dataSource[index][key] = res.content;
              this.setData({
                dataSource
              })
            })
          }
        }
      })
    },
    onRemove(event) {
      const {
        index
      } = event.currentTarget.dataset,
        dataSource = this.data.dataSource;
      wx.showModal({
        title: '要移除此项吗？',
        success: res => {
          if (res.confirm) {
            _remove(dataSource[index], () => {
              dataSource.splice(index, 1);
              this.setData({
                dataSource
              })
            })
          }
        }
      })
    },
    oningredientCancel() {
      this.setData({
        ingredientShow: false
      })
    },
    oningredientShow(event) {
      idex = event.currentTarget.dataset.index;
      this.setData({
        ingredientShow: true
      })
    },
    updateParams(value) {
      const {
        id,
        weight,
        defectAdjustmentId
      } = this.data.dataSource[idex];
      const ingredientsId = value['id'];
      _update({
        id,
        weight,
        defectAdjustmentId,
        ingredientsId
      }, () => {
        const dataSource = this.data.dataSource;
        dataSource[idex]['ingredientsId'] = ingredientsId;
        dataSource[idex]['_info']['name'] = value['name'];
        this.setData({
          dataSource
        })
      })
    },
    oningredientSelect(event) {
      if (this.data.modalShow) {
        const instance = this.selectComponent('#modal');
        instance?.onOpeningredientCallback(event.detail);
      } else {
        this.updateParams(event.detail)
      }
      this.oningredientCancel();
    },
    onModalCancel() {
      this.setData({
        modalShow: false
      })
    },
    onModalShow() {
      this.setData({
        modalShow: true
      })
    },
    fetchList(id) {
      console.log(this.properties['data'])
      _list({
        adjustmentGlazeTypeId: this.properties['data']['adjustmentGlazeTypeId'],
        defectAdjustmentId: id || this.properties['data']['id']
      }, res => {
        this.fetchIngredientsParentList();
        this.fetchIngredientsChildList(res);

      })
    },
    fetchIngredientsParentList() {
      ingParentList({}, res => {
        this.setData({
          ingredientsList: res
        })

      })
    },
    fetchIngredientsChildList(dataSource) {
      list({
        status: "ANNOUNCED"
      }, res => {
        this.setData({
          ingredientsChildList: res,
          dataSource: dataSource.map(item => {
            return {
              ...item,
              _info: res.find(it => {
                return it['id'] === item['ingredientsId']
              })
            }
          })
        })
      })
    },
    onModalSubmit(event) {
      const data = {
        ...event.detail,
        defectAdjustmentId: this.properties.data.id
      }
      _add(data, () => {
        this.fetchList()
      })
    },
  }
})